<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname="sc";

// 创建连接
$conn = mysqli_connect($servername, $username, $password,$dbname);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
/*//输出原始表格
$sql = 'SELECT * FROM 学生 WHERE 年龄 ';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h3>原始表格<h3>';
echo '<table border="1"><tr><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>出生日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['学号']}</td> ".
         "<td>{$row['姓名']} </td> ".
         "<td>{$row['性别']} </td> ".
         "<td>{$row['年龄']} </td> ".
         "<td>{$row['出生日期']} </td> ".
         "</tr>";
}
echo '</table>';*/
mysqli_query($conn , "set names utf8");
//作业(1)
$sql = 'SELECT * FROM 学生 WHERE 年龄 BETWEEN 18 AND 22';
 
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h3>作业(1)<h3>';
echo '<table border="1"><tr><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>出生日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['学号']}</td> ".
         "<td>{$row['姓名']} </td> ".
         "<td>{$row['性别']} </td> ".
         "<td>{$row['年龄']} </td> ".
         "<td>{$row['出生日期']} </td> ".
         "</tr>";
}
echo '</table>';
 
//作业(2)
mysqli_query($conn,"UPDATE 学生 SET 学号='081' WHERE 学号=001 ");
$sql = 'SELECT * FROM 学生 WHERE 年龄 ';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h3>作业(2)<h3>';
echo '<table border="1"><tr><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>出生日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['学号']}</td> ".
         "<td>{$row['姓名']} </td> ".
         "<td>{$row['性别']} </td> ".
         "<td>{$row['年龄']} </td> ".
         "<td>{$row['出生日期']} </td> ".
         "</tr>";
}
echo '</table>';

//作业(4)
$sql = 'SELECT * FROM 选课 ORDER BY 成绩 DESC;';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h3>作业(4)<h3>';
echo '<table border="1"><tr><td>学号</td><td>课程号</td><td>成绩</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['学号']}</td> ".
         "<td>{$row['课程号']} </td> ".
         "<td>{$row['成绩']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>